package level;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.Scanner;

import affichage.fabriqueTexture;



public class Plateau {
	
	private ArrayList<mulotPerso> mulots = new ArrayList<mulotPerso>(); 
	private iTexture[][] plateau;
	private int mulotACreer, mulotCreer = 0, mulotASauver, nbMulotSauve = 0;
	private int xE, yE, xS, yS, nLevel;
	private boolean creerMulot = false;
	boolean start = true;
	
	//Getter et setter
	public int getMulotASauver() {
		return mulotASauver;
	}
	public int getNbMulotSauve() {
		return nbMulotSauve;
	}
	public int getnLevel() {
		return nLevel;
	}
	public mulotPerso getMulots(int n) {
		assert(n >= 0 && n < mulots.size());
		return mulots.get(n);
	}

	public Plateau(String name, int xMax, int yMax, int mulotACreer, int mulotASauver, int nLevel) {
		
		this.mulotASauver = mulotASauver;
		this.mulotACreer = mulotACreer;
		this.nLevel = nLevel;
		
		try
		{
		    File f = new File (name);
		    Scanner scanner = new Scanner (f);

		        try
		        {
		            
		            plateau = new iTexture[xMax][yMax];
		            
		            lecturePlateau(name, scanner);
		        }
		        catch (NoSuchElementException e)
		        {
		        	e.printStackTrace();
		        }
		 
		    scanner.close();
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
	}
	
	public void dessiner(){
		int i = 0;
		for(mulotPerso m : mulots){
			m.afficher(this.plateau, i);
			++i;
		}
	}
	
	public boolean finNiveau(){
		if(nbMulotEnJeu() == 0 && !start){
			return true;
		}
		return false;
	}
	
	public void effacer(){
		for(mulotPerso m : mulots){
			m.effacer(this.plateau);
		}
	}
	
	private int nbMulotEnJeu(){
		int y = 0;
		for(mulotPerso m : mulots){
			if(m.utile()){
				++y;
			}
		}
		return y;
	}
	
	public boolean levelWin(){
		if(nbMulotSauve >= mulotASauver)
			return true;
		return false;
	}
	
	public void supMulot(int i){
		mulots.remove(i);
	}
	
	public void mulotsSauve(){
		for(int i = 0; i < mulots.size(); ++i){
			if(mulots.get(i).getX() == xS && mulots.get(i).getY() == yS){
				supMulot(i);
				++nbMulotSauve;
			}
		}
	}
	
	public void deplacer(){
		int i=0;
		for(;i<mulots.size();++i){
			mulots.get(i).deplacer(this);
		}
	}
	
	public void creerMulot(){
		if(mulotCreer < mulotACreer && !creerMulot){
			mulotPerso m = new mulotPerso();
			mulots.add(m);
			start = false;
			creerMulot = true;
			++mulotCreer;
		}else
			creerMulot = false;
	}
	
	public int getNbMulots() {
		return mulots.size();
	}

	public iTexture[][] getPlateau() {
		return plateau;
	}
	
	private void lecturePlateau(String name, Scanner scan){
		String ligne;
		int x = 0, y = 0;
	   
        try
        {
            while(scan.hasNextLine()){
            	ligne = scan.nextLine();
            	for(int i = 0; i < ligne.length(); ++i){
            		plateau[x][y] = fabriqueTexture.fabriquer(ligne.charAt(i));
            		if(ligne.charAt(i) == 'E'){
            			xE = x;
            			yE = y;
            		}
            		if(ligne.charAt(i) == 'S'){
            			xS = x;
            			yS = y;
            		}
            		++x;
            	}
            	++y;
            	x = 0;
            }
        }
        catch (NoSuchElementException e)
        {
        	e.printStackTrace();
        }
	}
}
